package com.merelyb.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 程振奇 on 2017/10/16.
 */
public class QrcodeUtil {
    private static final Logger log = LoggerFactory.getLogger(QrcodeUtil.class);

    /**
     * 保存二维码图片(google)
     *
     * @param sContent   内容
     * @param sFileName  文件名称
     * @param sCharacter 编码格式
     * @param iWidth     宽度
     * @param iHeight    高度
     * @return
     */
    public static boolean encodeQRCodeToFile(String sContent, String sFileName, String sCharacter,
                                      int iWidth, int iHeight) {
        boolean bReturn = true;
        String sPrefix = sFileName.substring(sFileName.lastIndexOf(".") + 1); //获取扩展名
        Map<EncodeHintType, Object> ehtHints = new HashMap<EncodeHintType, Object>();
        ehtHints.put(EncodeHintType.CHARACTER_SET, sCharacter);
        BitMatrix bitMatrix = null;// 生成矩阵
        try {
            bitMatrix = new MultiFormatWriter().encode(sContent,
                    BarcodeFormat.QR_CODE, iWidth, iHeight, ehtHints);

            Path path = FileSystems.getDefault().getPath(sFileName);
            //        sPrefix 异常还没有处理
            try {
                MatrixToImageWriter.writeToPath(bitMatrix, sPrefix, path);// 输出图像
            } catch (IOException e) {
                bReturn = false;
                log.error("输出二维码图像出现问题",e);
            }

        } catch (WriterException e) {
            bReturn = false;
            log.error("输出二维码图像出现问题",e);
        }
        return bReturn;
    }

    /**
     * 保存二维码流(google)
     *
     * @param sContent     内容
     * @param sCharacter   编码
     * @param sPrefix      扩展名
     * @param iWidth       宽度
     * @param iHeight      高度
     * @param outputStream 返回流
     * @return
     */
    public static boolean encodeQRCodeToStream(String sContent, String sCharacter, String sPrefix, int iWidth, int iHeight,
                                        OutputStream outputStream) {
        boolean bReturn = true;
        Map<EncodeHintType, Object> ehtHints = new HashMap<EncodeHintType, Object>();
        ehtHints.put(EncodeHintType.CHARACTER_SET, sCharacter);
        BitMatrix bitMatrix = null;// 生成矩阵
        try {
            bitMatrix = new MultiFormatWriter().encode(sContent,
                    BarcodeFormat.QR_CODE, iWidth, iHeight, ehtHints);

            try {
                //        sPrefix 异常还没有处理 主要为扩展名问题
                MatrixToImageWriter.writeToStream(bitMatrix, sPrefix, outputStream);
            } catch (IOException e) {
                bReturn = false;
                log.error("输出二维码图像出现问题",e);
            }

        } catch (WriterException e) {
            bReturn = false;
            log.error("输出二维码图像出现问题",e);
        }
        return bReturn;
    }

    /**
     * 解析图片(google)
     *
     * @param image      传入图片
     * @param sCharacter //编码方式
     * @return
     */
    public static String decodeQRCodeFromImage(BufferedImage image, String sCharacter) {
        String sReturn = "";
        try {
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
            Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
            hints.put(DecodeHintType.CHARACTER_SET, sCharacter);
            Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
            sReturn = result.getText();
        } catch (NotFoundException e) {
            log.error("解析二维码出现问题",e);
        }
        return sReturn;
    }

}
